using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;
using NTTHAO.CommonWeb;

public partial class admin_ImageListProvider : System.Web.UI.Page
{
    public string ROOT_PATH = "/" + Constant.UPLOAD_FILE_PATH + "Files/";
    public static string FILES_MESSAGE = "FILES_MESSAGE";
    public static string FILES_MESSAGE_TYPE = "FILES_MESSAGE_TYPE";

    public string[] _ImageFilesList = new string[0];

    public string _CurrentPath = "/";
    public string[] _DirectoryList = new string[0];

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.ContentType = "text/html";
        //Response.Write(Session[Constant.SITE_SESSION] == null ? "null" : ((NTTHAO.Web.BL.CommonPage.CommonSite)Session[Constant.SITE_SESSION]).SiteID);
        //Response.Write(NTTHAO.CommonWeb.WebGlobal.GetSite(System.Web.HttpContext.Current.Session).SiteID);
        
        try
        {
            if (Request.QueryString["file"] != null)
            {
                _CurrentPath = Request.QueryString["file"];
            }
            _CurrentPath = "/" + _CurrentPath.TrimStart('/');
            _CurrentPath = _CurrentPath.TrimEnd('/') + "/";

            string rootServerPath = Server.MapPath(ROOT_PATH);
            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.TrimStart('/').Replace("/", "\\"));

            if (!Directory.Exists(currentServerPath))
            {
                Response.Write(currentServerPath);
                return;
            }

            _DirectoryList = Directory.GetDirectories(currentServerPath);
            if (_DirectoryList != null)
            {
                for (int i = 0; i < _DirectoryList.Length; i++)
                {
                    _DirectoryList[i] = Path.GetFileName(_DirectoryList[i]);
                }
            }
            _ImageFilesList = this.GetImageList(currentServerPath);

            string jsonString = this.GetJSONString();
            Response.ClearContent();
            Response.Write(jsonString);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message + "<br /><br />" + ex.StackTrace);
        }
    }

    public string GetJSONString()
    {
        string foldersString = "";
        string fileString = "";
        for(int i = 0; i < this._DirectoryList.Length; i++)
        {
            string folder = this._DirectoryList[i];
            if (i > 0)
            {
                foldersString += ",";
            }
            foldersString += "{\"src\":\"" + folder.Replace("\"", "\\\"") + "\"}";
        }
        for (int i = 0; i < this._ImageFilesList.Length; i++)
        {
            string file = this._ImageFilesList[i];
            if (i > 0)
            {
                fileString += ",";
            }
            fileString += "{\"src\":\"" + file.Replace("\"", "\\\"") + "\"}";
        }

        string result = "{\"id\":\""
            + (Request.QueryString["id"] == null ? "fck" : Request.QueryString["id"])
            + "\",\"absolute_folder\":\"" + ROOT_PATH.TrimEnd('/') + this._CurrentPath
            + "\", \"related_folder\":\"" + this._CurrentPath
            + "\",\"folders\":[" + foldersString
            + "],\"images\":[" + fileString
            + "]}";
        //return string.Format(jsonTemp, foldersString, fileString, ROOT_PATH.TrimEnd('/') + this._CurrentPath, this._CurrentPath, Request.QueryString["id"]);
        return result;
    }

    public string[] GetImageList(string folderPath)
    {
        SortedList<string, string> imageList = new SortedList<string, string>();
        string[] filters = Constant.IMAGE_FILE_EXTENTIONS.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string filter in filters)
        {
            string[] files = Directory.GetFiles(folderPath, "*" + filter);
            if (files != null && files.Length > 0)
            {
                foreach (string file in files)
                {
                    string fName = Path.GetFileName(file);
                    if (imageList.IndexOfKey(fName) < 0)
                    {
                        imageList.Add(fName, fName);
                    }
                }
            }
        }

        string[] ret = new string[imageList.Count];
        imageList.Values.CopyTo(ret, 0);
        return ret;
    }

    public string ParentPath
    {
        get
        {
            if (_CurrentPath == "/")
            {
                return null;
            }

            string parentPath = _CurrentPath.Substring(0, _CurrentPath.Length - 1);
            parentPath = parentPath.Substring(0, parentPath.LastIndexOf("/") + 1);

            return parentPath;
        }
    }
}
